package com.activenetwork.smlite.testcase.events.createEvents.editEvents;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.testcase.events.createevents.editevents.editevents013.EditEvents013DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.page.event.EventDetailView.EventSourceSubview;
import com.activenetwork.smlite.page.event.EventDetailView.EventSummarySubview;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;


public class EditEvents013 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(EditEvents013.class);
	private EditEvents013DataType data;

	@BeforeClass(alwaysRun = true)
	public void dataSetup() {		
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(EditEvents013.class);
			data = helper.bind(EditEvents013DataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}

	@Test
	public void Verify_tags_cen_be_edited_successfully() {
		loginBusiness.login();

		logger.info("Create a new event with location first.");
		EventBusiness eventBusiness = new EventBusiness();
		try{
			eventBusiness.deleteEvent(data.getEventInfo().getBasic().getTitle(), data.getEventInfo());
		}catch(RuntimeException e){
			logger.info("The event " + data.getEventInfo().getBasic().getTitle() + " does not exist!");
		}
		eventBusiness.addEvent(data.getEventInfo());
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.searchEvent(data.getEventInfo().getBasic().getTitle(), data.getEventInfo());
		EventSummarySubview eventSummarySubview = PageLoader.load(EventSummarySubview.class);
		EventBasicSubview eventBasicSubview = eventSummarySubview.showBasic();
		
		//expand Advanced tag form
		eventBasicSubview = eventBasicSubview.showEventTag();
		eventBasicSubview = eventBasicSubview.removeAllTag();


		eventBasicSubview = eventBasicSubview.selectEventTagAJAX("test",1);
		EventSourceSubview eventSourceSubview = eventBasicSubview.nextStep();
		eventBasicSubview = eventSourceSubview.goBackBasicSubview();
		eventBasicSubview = eventBasicSubview.showEventTag();
		List<String> tagNames = eventBasicSubview.getTagName();
		
		boolean found = false;
		for(String name : tagNames){
			if(StringUtils.containsIgnoreCase(name, "test")){
				found = true;
			}
		}
		
		if(!found){
			EnvContext.failurePointTL.get().add(new FailurePoint(
				EditEvents013.class.getName(),
				"Verify tag can be edited when create events",
				"Tag name can not be modified."));
		}

		
		loginBusiness.logout();
		FailureAssertion.pop(EditEvents013.class);
	}

}
